﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace OnlineEducation.Extensions
{
    public class UserTokenAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                
                
            }
             
            base.OnActionExecuting(filterContext);
        }

        
    }
        //protected override void HandleUnauthorizedRequest(HttpActionContext filterContext)
        //{

        //    base.HandleUnauthorizedRequest(filterContext);
        //    var response = filterContext.Response = filterContext.Response ?? new HttpResponseMessage();
        //    response.StatusCode = HttpStatusCode.Forbidden;
        //    ResultModel model = new ResultModel();

        //    model.result_code = ResultCode.Error;
        //    model.result_data = null;
        //    model.result_mess = "您没有授权!";

        //    response.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
        //}
        //public override void OnAuthorization(HttpActionContext actionContext)
        //{
        //    //从http请求的头里面获取身份验证信息，验证token值是否有效
        //    string token = GetToken(actionContext);

        //    if (!string.IsNullOrEmpty(token))
        //    {
        //        if (UserService.Instance.ValidateToken(token))
        //        {
        //            base.IsAuthorized(actionContext);
        //        }
        //        else
        //        {
        //            HandleUnauthorizedRequest(actionContext);
        //        }
        //    }

        //    else//如果取不到token值，并且不允许匿名访问，则返回未验证401
        //    {
        //        var attributes = actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().OfType<AllowAnonymousAttribute>();
        //        bool isAnonymous = attributes.Any(a => a is AllowAnonymousAttribute);//验证当前动作是否允许匿名访问
        //        if (isAnonymous)
        //        {
        //            base.OnAuthorization(actionContext);
        //        }
        //        else
        //        {
        //            HandleUnauthorizedRequest(actionContext);
        //        }
        //    }
        //}
        //public string GetToken(HttpActionContext actionContext)
        //{
        //    if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request["token"]))
        //        return System.Web.HttpContext.Current.Request["token"].ToString();
        //    if (System.Web.HttpContext.Current.Request.InputStream.Length > 0)
        //    {
        //        string json = new System.IO.StreamReader(System.Web.HttpContext.Current.Request.InputStream).ReadToEnd();
        //        Dictionary<string, object> di = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
        //        if (di.Keys.Contains("token")) return di["token"].ToString();
        //    }
        //    return "";
        //}

           
    //}
}
